package com.xs.tool.json;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class StringKeyMapDeserializer implements ObjectDeserializer {

    @Override
    public Map<String, Object> deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        Map<Object, Object> originalMap = parser.parseObject(HashMap.class);
        Map<String, Object> resultMap = new HashMap<>();

        if (originalMap != null) {
            originalMap.forEach((k, v) -> resultMap.put(k.toString(), v));
        }

        return resultMap;
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}